#include "wds_mainmenu.h"
#include "wds_ui_animator.h"
#include "wds_ui_popupanimator.h"
#include "wds_ui_helper.h"
#include "gameflow/game_flow_manager.h"
#include "wds_sfx_manager.h"

#include "application/game_application.h"
#include "application/game_rms.h"
#include "audio/audio.h"
#include "gameflow/ui_popup.h"

#include "util/log.h"				// log

#ifndef _PLATFORM_WIN32
//std::thread		WDSMainMenu::m_Thread;
#endif

#ifdef _PLATFORM_ANDROID
#include "SDL2/src/core/android/SDL_android.h"
#include "android/sleipner_android.h"
#endif

#ifdef _PLATFORM_IOS
#include "application/ios_wrappers.h"
#endif


WDSMainMenu::WDSMainMenu(camera::FbCamera*	pCamera) : gameflow::UIState(pCamera,"ui/main_menu.ui"),						
						m_IsFinishAddingMapState(false),
						m_IsLoadedSound(false),
						m_IsLoadedEffect(false)

{
	//SetupTransition("button_tomap", "map");

	m_pUILogo = GetWidget("title_logo");
	m_pUIButtonsFrame = GetWidget("adjustmentframe");
	m_pUIChar = GetWidget("char");

	if(m_pUIChar)
	{
		WDSUIHelper::ResizeToCurentAspectRatio(m_pUIChar);		
		m_pUIChar->SetPosition( math::Vec2(0.5f, 1.0f - (m_pUIChar->GetSize().y * 0.5f)) );
	}


	WDSUIHelper::ResizeToCurentAspectRatio(GetWidget("title_logo"));

	//SetWidgetPopup("button_achievements","ui/achievement.ui");

	SetWidgetFunctor("SETTINGS_2",UI_DELEGATE(WDSMainMenu, OnButtonSettings));
	//SetWidgetFunctor("toFB",UI_DELEGATE(WDSMainMenu, OnButtonGotoFacebook));
	//SetWidgetFunctor("toTwitter",UI_DELEGATE(WDSMainMenu, OnButtonGotoTwitter));
	//SetWidgetFunctor("to2DStar",UI_DELEGATE(WDSMainMenu, OnButtonGoto2DStar));
	SetWidgetFunctor("START_GAME",UI_DELEGATE(WDSMainMenu, OnButtonGotoMap));
	
	SetWidgetFunctor("button_moregames",UI_DELEGATE(WDSMainMenu, OnButtonMoreGames));
	SetWidgetFunctor("button_rateus",UI_DELEGATE(WDSMainMenu, OnButtonRateUS));
	

	

	//m_pUIFacebook = GetWidget("toFB");
	//m_pUITwitter = GetWidget("toTwitter");
	//m_pUI2DStar = GetWidget("to2DStar");
	
	
	
	m_pButtons.push_back(new WDSUIHelper(GetWidget("START_GAME")));
	m_pButtons.push_back(new WDSUIHelper(GetWidget("SETTINGS_2")));
	//m_pButtons.push_back(new WDSUIHelper(GetWidget("button_achievements")));
	m_pButtons.push_back(new WDSUIHelper(GetWidget("button_moregames")));
	m_pButtons.push_back(new WDSUIHelper(GetWidget("button_rateus")));
		
	m_Music = "sounds/theme_menu.ogg";	
}

WDSMainMenu::~WDSMainMenu()
{
	for(int i = 0; i < (int)m_pButtons.size(); i++)
	{
		UTIL_SAFE_DELETE(m_pButtons[i]);
	}
	m_pButtons.resize(0);
}

void LoadSound();


bool	WDSMainMenu::OnEnter()
{
    
	
	if(m_pUIButtonsFrame)
		m_pUIButtonsFrame->SetPosition(math::Vec2(0.75f,0));

	
	//WDSAnimationManager::Instance().ForceFinish();

	gameflow::UIPopupManager::Instance().HideAllPopups();


	//pthread_t  t1;
	//int create1 = pthread_create( &t1, NULL, function1);

	AUDIO().SetSFXVolume((int)g_pApp->GetRMS()->GetSoundVolume());
    AUDIO().SetMusicVolume((int)g_pApp->GetRMS()->GetMusicVolume());
	if(!m_IsLoadedSound)
	{
				
//#ifndef _PLATFORM_WIN32
 //    m_Thread =  std::thread(LoadSound); 
	 //m_Thread.join();
//#else
	// No multithreading for now
	LoadSoundStatic();
//#endif
	m_IsLoadedSound = true;

	}

   bool ReturnV = gameflow::UIState::OnEnter();
   return ReturnV;

}

void LoadSound() 
{
	WDSMainMenu::LoadSoundStatic();
}

void			WDSMainMenu::LoadSoundStatic()
{
	 //AUDIO().Init("sounds/soundbank.lua");
	_LOG(MSG_INIT, "Audio manager Init");
#ifndef _PLATFORM_WIN32
	//m_Thread.join();
#endif
	//AUDIO().PlayMusic("sounds/theme_menu.ogg");
}

bool	WDSMainMenu::OnExit()
{
	if(!m_IsLoadedEffect)
	{
		//WDSSFXManager::Instance().Cache();
		m_IsLoadedEffect = true;
	}

	return gameflow::UIState::OnExit();
}

bool	WDSMainMenu::Tick(float dt)
{
	if(!m_pUILogo)
		return gameflow::UIState::Tick(dt);
	
	const float TARGET = m_pUILogo->GetSize().y * 0.5f;
	if( m_pUILogo && m_pUILogo->GetPosition().y > TARGET  )
	{
		m_pUILogo->SetPosition( m_pUILogo->GetPosition() - math::Vec2(0,1.0f) * dt * 0.75f );
		if(m_pUILogo->GetPosition().y < 0.1f)
			m_pUILogo->SetPosition( math::Vec2(m_pUILogo->GetPosition().x, TARGET ));

	}
	else
	{

		if(m_pUIButtonsFrame && m_pUIButtonsFrame->GetPosition().x > 0)
		{
			m_pUIButtonsFrame->SetPosition(m_pUIButtonsFrame->GetPosition() - math::Vec2(1.0f,0) * dt * 3.0f ) ;
			if( m_pUIButtonsFrame->GetPosition().x < 0)
				m_pUIButtonsFrame->SetPosition(math::Vec2(0,0)) ;
		}	
		else
		{
			
		}
	}
	
	WDSUIAnimator::Instance().Tick(dt);

	for(int i = 0; i < (int)m_pButtons.size(); i++)
	{
		m_pButtons[i]->Tick(dt);
	}

	WDSUIPopupAnimatorMgr::Instance().Tick(dt);
	return gameflow::UIState::Tick(dt);
}

/*
void DELEGATE_CALL		WDSMainMenu::OnButtonGotoFacebook(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    //FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.facebook.com/2dstar");
   
#ifdef _PLATFORM_ANDROID
    //Android_JNI_GoToURL((int)ANDROID_JNI_URL_FACEBOOK);
#endif

}

void DELEGATE_CALL		WDSMainMenu::OnButtonGotoTwitter(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    //FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.twitter.com/2dstar");
#ifdef _PLATFORM_ANDROID
   // Android_JNI_GoToURL((int)ANDROID_JNI_URL_TWITTER);
#endif

}
	
void DELEGATE_CALL		WDSMainMenu::OnButtonGoto2DStar(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    //FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.2dstar.com");
#ifdef _PLATFORM_ANDROID
   // Android_JNI_GoToURL((int)ANDROID_JNI_URL_2DSTAR);
#endif

}
*/

void DELEGATE_CALL		WDSMainMenu::OnButtonSettings(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	WDSUIPopupAnimatorMgr::Instance().Show(WDSUIPopupAnimatorMgr::POPUP_NAMES_SETTING);
}

void DELEGATE_CALL		WDSMainMenu::OnButtonGotoMap(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	
	gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","map","",NULL);
	
}

void	WDSMainMenu::KeyPressed(input::Keyboard::KEY* pKey)
{
	if(WDSUIPopupAnimatorMgr::Instance().KeyPressed(pKey))
	{
		// taken
	}	
	else if(pKey->KeyID == KEY_ESCAPE)
	{
#ifdef _NOLOCALIZATION_
		gameflow::UIPopupManager::Instance().StartYesNo(QUIT_YESNO, UI_DELEGATE(WDSMainMenu, OnHardQuit), NULL);
#else
		gameflow::UIPopupManager::Instance().StartYesNo("QUIT_YESNO", UI_DELEGATE(WDSMainMenu, OnHardQuit), NULL);
#endif

		WDSUIPopupAnimatorMgr::Instance().Show(WDSUIPopupAnimatorMgr::POPUP_NAMES_YESNO);
	}
}
	
void DELEGATE_CALL	WDSMainMenu::OnHardQuit(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	if(g_pApp)
		g_pApp->GetHandler()->DestroyWindow();

#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_onHardQuit, 0);
#endif
}

void DELEGATE_CALL	WDSMainMenu::OnButtonRateUS(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_gotoURL, 0);
#endif
    
#ifdef _PLATFORM_IOS
    GotoWebSite("https://itunes.apple.com/us/app/match5-hd/id578093384?ls=1&mt=8");
#endif
}
	
void DELEGATE_CALL	WDSMainMenu::OnButtonMoreGames(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
#ifdef _PLATFORM_ANDROID
	Android_JNI_SendMessage(FUNC_gotoURL, 1);
#endif
    
#ifdef _PLATFORM_IOS
    GotoWebSite("http://www.facebook.com/2dstar");
#endif
}